/**
 * @file makeStrins.js
 * @author GaoFeng <gaofeng@Antiy.cn>
 * @createTime 2018-01-23 13:46:07
 * @description summary & oneString
 * 可配置的一句话描述基本完成，未来替换这个文件即可，此文件不在进行过多重构。
 */

import { translateVar } from './maps/varAbbMap'

/**
 * @description 判断属性是否存在
 * @param {any} ele
 * @returns {Boolean}
 */
const isNone = ele => {
  let _type = Object.prototype.toString.call(ele).slice(8, -1)
  return _type === 'Undefined' || _type === 'Null'
}

function transformNone (data) {
  return isNone(data) ? '' : data
}

// 获取ip对应资产的index，资产不存在返回-1
function getAssetIn (data, ip) {
  if (isNone(data.asset) || data.asset.length === 0) {
    return -1
  }
  for (let [idx] of data.asset.entries()) {
    if (ip === data.asset[idx].asset_ip) {
      return idx
    }
  }
  return -1
}

// 获取对应index的资产信息
function getAsset (data, index) {
  if (isNone(data.asset[index].node_des) || data.asset[index].node_des === '') {
    return `资产 <span class="asset">${transformNone(data.asset[index].asset_ip)}</span>`
  } else {
    return `资产 <span class="asset">${transformNone(data.asset[index].asset_ip)}</span>（<span class="normal">${transformNone(data.asset[index].node_des)}</span>）`
  }
}

// 获取全部资产的ip
function getAssetIp (data) {
  if (data.asset != null) {
    let ans = []
    for (let el of data.asset) {
      ans.push(el.asset_ip)
    }
    return ans
  }
}

function getDstLocationPTD (data) {
  if (data.dst.country !== data.dst.area) {
    return `<span class="asset">${transformNone(data.dst.ip)}</span>, <span class="asset">${transformNone(data.dst.ip)}（<span class="normal">${transformNone(data.dst.country)}${transformNone(data.dst.area)}</span> ）</span>`
  } else if (!isNone(data.dst.country) && data.dst.country.length !== 0) {
    return `<span class="asset">${transformNone(data.dst.ip)}</span>, <span class="asset">${transformNone(data.dst.ip)}（<span class="normal">${data.dst.country}</span>）</span>`
  } else {
    return `终端 <span class="asset">${transformNone(data.dst.ip)}</span>, <span class="asset">${transformNone(data.dst.ip)}</span>`
  }
}

function getSrcLocationPTD (data) {
  if (data.src.country !== data.src.area) {
    return `<span class="asset">${transformNone(data.src.ip)}</span>（<span class="normal">${transformNone(data.src.country)}${transformNone(data.src.area)}</span>）`
  } else if (!isNone(data.dst.country) && data.src.country.length !== 0) {
    return `<span class="asset">${transformNone(data.src.ip)}</span>（<span class="normal">${data.src.country}</span> ）`
  } else {
    return `终端 <span class="asset">${transformNone(data.src.ip)}</span>`
  }
}

function getClient (data) {
  if (!isNone(data.client.name)) {
    return `<span class="asset">${transformNone(data.client.ip)}</span>（<span class="normal">${transformNone(data.client.name)}</span>）`
  } else {
    return `主机 <span class="asset">${transformNone(data.client.ip)}</span>`
  }
}

function getMalicious (data) {
  let level = -1
  let index = 0
  for (let i in data.d.file) {
    if (data.d.file[i].level > level) {
      index = i
    }
  }
  if (data.d.file.length > 1) {
    return `发现 <span class="virus">${data.d.file[index].md5}</span> 恶意代码等攻击, 恶意代码名称为 <span class="virus">${data.d.file[index].malname}</span> 等, 恶意代码家族为 <span class="virus">${data.d.file[index].virus_family}</span> 等`
  } else {
    return `发现 <span class="virus">${data.d.file[index].md5}</span> 恶意代码攻击, 恶意代码名称为 <span class="virus">${data.d.file[index].malname}</span> , 恶意代码家族为 <span class="virus">${data.d.file[index].virus_family}</span>`
  }
}

function getMaliciou (data) {
  let level = -1
  let index = 0
  for (let i in data.d.file) {
    if (data.d.file[i].level > level) {
      index = i
    }
  }
  if (data.d.file.length > 1) {
    return `<span class="virus">${data.d.file[index].malname}</span> 等`
  } else {
    return `<span class="virus">${data.d.file[index].malname}</span>`
  }
}

function getMd5 (data) {
  let level = -1
  let index = 0
  for (let i in data.d.file) {
    if (data.d.file[i].level > level) {
      index = i
    }
  }
  if (data.d.file.length > 1) {
    return `<span class="virus">${data.d.file[index].md5}</span> 等`
  } else {
    return `<span class="virus">${data.d.file[index].md5}</span>`
  }
}

function getDomainUrl (data) {
  let ans = ''
  if (data.domain && data.domain.length !== 0) {
    ans = `${ans} <span class="shallow">, 域名为</span> `
    for (let i in data.domain) {
      ans = `${ans} <span class="link">${data.domain[i].name}</span>,`
    }
    // 最后一条信息删除结尾逗号
    ans = ans.slice(0, -1)
  }
  if (data.url && data.url.length !== 0) {
    ans = `${ans} <span class="shallow">, URL为</span> `
    for (let i in data.url) {
      ans = `${ans} <span class="link">${data.url[i]}</span>,`
    }
    ans = ans.slice(0, -1)
  }
  return ans
}

function getLocationInfo (data) {
  let location = ''
  if (transformNone(data.country) === transformNone(data.area)) {
    location = `${transformNone(data.country)}`
  } else if (transformNone(data.area) === transformNone(data.city)) {
    location = `${transformNone(data.country)} ${transformNone(data.area)}`
  } else {
    location = `${transformNone(data.country)} ${transformNone(data.area)} ${transformNone(data.city)}`
  }
  return location
}

function iepOption (data) {
  let ans
  switch (data) {
    case 1:
      ans = '拷入'
      break
    case 2:
      ans = '拷出'
      break
    case 3:
      ans = '重命名'
      break
    case 4:
      ans = '删除'
      break
    case 5:
      ans = '新增'
      break
    case 6:
      ans = '移动'
      break
    case 7:
      ans = '执行'
      break
    case 8:
      ans = '修改'
      break
    default:
      ans = data
      break
  }
  return ans
}

let firstAns = ''
let sendAns = ''
const desConfig = [
  // PTD
  {
    pid: 'PTD',
    useKey: 'etype',
    useValue: 'maliciouscode',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let location = getAssetIn(data, data.dst.ip)
      location === -1 ? (firstAns = getDstLocationPTD(data)) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.src.ip)
      location === -1 ? (sendAns = getSrcLocationPTD(data)) : (sendAns = getAsset(data, location))
      let beacon = getDomainUrl(data)
      let proto = data.proto ? data.proto : 'UNKNOW'
      if (!isNone(data.file[0])) {
        return `${sendAns} 通过 <span class="proto">${proto}</span> 协议访问 ${firstAns},发现<span class="virus">${data.file[0].md5}</span>等恶意代码传输行为${beacon}`
      } else {
        return `${sendAns} 通过 <span class="proto">${proto}</span> 协议访问 ${firstAns},发现恶意代码传输行为<span class="virus">${beacon}</span>`
      }
    }
  },
  {
    pid: 'PTD',
    useKey: 'etype',
    useValue: 'c2',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let location = getAssetIn(data, data.dst.ip)
      location === -1 ? (firstAns = '') : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.src.ip)
      location === -1 ? (sendAns = '') : (sendAns = getAsset(data, location))
      if (firstAns === '' && sendAns === '') {
        return `发现C&C攻击,病毒名称为 <span class="virus">${data.malname[0]}</span>`
      } else if (firstAns !== '' && sendAns !== '') {
        return `${firstAns}, ${sendAns}遭受C&C攻击,病毒名称为<span class="virus">${data.malname[0]}</span>`
      } else {
        return `${firstAns}${sendAns}遭受C&C攻击,病毒名称为<span class="virus">${data.malname[0]}</span>`
      }
    }
  },
  {
    pid: 'PTD',
    useKey: 'etype',
    useValue: 'net-intrusion',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let proto = data.proto ? data.proto : 'UNKNOW'
      let location = getAssetIn(data, data.dst.ip)
      location === -1 ? (firstAns = '') : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.src.ip)
      location === -1 ? (sendAns = '') : (sendAns = getAsset(data, location))
      return `发现通过 <span class="proto">${proto}</span> 发起的网络入侵攻击,相关资产为${firstAns} ${sendAns}`
    }
  },
  {
    pid: 'PTD',
    useKey: 'etype',
    useValue: 'record',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let proto = data.proto ? data.proto : 'UNKNOW'
      let location = getAssetIn(data, data.dst.ip)
      location === -1 ? (firstAns = getDstLocationPTD(data)) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.src.ip)
      location === -1 ? (sendAns = getSrcLocationPTD(data)) : (sendAns = getAsset(data, location))
      let beacon = getDomainUrl(data)
      return `${sendAns} 通过 <span class="proto">${proto}</span> 协议访问 ${firstAns}${beacon}`
    }
  },
  // PTA
  {
    pid: 'PTA',
    useKey: 'pid',
    useValue: 'PTA',
    summary: function (data) {
      return `文件 <span class="virus">${data.md5}</span> 深度分析详情`
    },
    oneString: function (data) {
      if (data.is_malicious === false) {
        return `文件 <span class="virus">${data.md5}</span> 经追影深度鉴定，未发现恶意行为`
      } else {
        let ip = ''
        if (data.ip.length > 0 || data.domain.length > 0 || data.url.length > 0) {
          ip = `发现与 <span class="virus">${data.ip[0]}</span> 等IP,发生通讯的网络行为`
        }
        return `文件 <span class="virus">${data.md5}</span> 经追影深度鉴定，确定为恶意代码，病毒名称为 <span class="virus">${data.malname}</span> ${ip}`
      }
    }
  },
  // IEP
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'virus_detect',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      if (isNone(data.asset) || data.asset.length === 0) {
        firstAns = getClient(data)
      } else {
        firstAns = getAsset(data, 0)
      }
      return `${firstAns} ${getMalicious(data)}`
    }
  },
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'loophole_detect',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      if (isNone(data.asset) || data.asset.length === 0) {
        firstAns = getClient(data)
      } else {
        firstAns = getAsset(data, 0)
      }
      if ((data.patch.ms.length === 0 || isNone(data.patch.ms[0])) && (data.patch.cve.length === 0 || isNone(data.patch.cve[0]))) {
        return `${firstAns} 存在漏洞, 需更新补丁 <span class="patch">${data.patch.kbid}</span>`
      } else if ((data.patch.ms.length !== 0 || isNone(data.patch.ms[0])) && (data.patch.cve.length === 0 || isNone(data.patch.cve[0]))) {
        return `${firstAns} 存在漏洞 <span class="virus">${data.patch.ms}</span> <span class="virus">${data.patch.cve}</span>, 需更新补丁 <span class="patch">${data.patch.kbid}</span>`
      } else {
        return `${firstAns} 存在漏洞 <span class="virus">${data.patch.ms}</span>, <span class="virus">${data.patch.cve}</span>, 需更新补丁 <span class="patch">${data.patch.kbid}</span>`
      }
    }
  },
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'dev_detect',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      if (isNone(data.asset) || data.asset.length === 0) {
        firstAns = getClient(data)
      } else {
        firstAns = getAsset(data, 0)
      }
      return `${firstAns} 接入移动设备 <span class="asset">${data.mobile_dev.name}</span>`
    }
  },
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'dev_use_detect',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      if (isNone(data.asset) || data.asset.length === 0) {
        firstAns = getClient(data)
      } else {
        firstAns = getAsset(data, 0)
      }
      let option = iepOption(data.mobile_use_dev.option)
      let md5 = []
      for (let i in data.file) {
        md5.push(data.file[i].md5)
      }
      return `${firstAns} 使用移动设备 <span class="asset">${data.mobile_use_dev.name}</span> ${option} 文件 <span class="virus">${md5}</span>`
    }
  },
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'proc_protect',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      if (isNone(data.asset) || data.asset.length === 0) {
        firstAns = getClient(data)
      } else {
        firstAns = getAsset(data, 0)
      }
      return `${firstAns} ${getMalicious(data)}`
    }
  },
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'active_protect',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      if (isNone(data.asset) || data.asset.length === 0) {
        firstAns = getClient(data)
      } else {
        firstAns = getAsset(data, 0)
      }
      return `${firstAns} ${getMalicious(data)}`
    }
  },
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'border_monitor',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      if (isNone(data.asset) || data.asset.length === 0) {
        firstAns = getClient(data)
      } else {
        firstAns = getAsset(data, 0)
      }
      return `${firstAns} 遭受恶意代码 <span class="virus">${data.fileinfo.md5}</span> 攻击，恶意代码名称为 <span class="virus">${data.fileinfo.malname}</span>（域名为<span class="link">${data.fileinfo.domain}</span>，url为<span class="link">${data.fileinfo.url}</span>）`
    }
  },
  {
    pid: 'IEP',
    useKey: 'type',
    useValue: 'network_monitor',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let location = getAssetIn(data, data.dst.ip)
      location === -1 ? (firstAns = getDstLocationPTD(data)) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.src.ip)
      location === -1 ? (sendAns = getSrcLocationPTD(data)) : (sendAns = getAsset(data, location))
      return `${sendAns} 通过 <span class="proto">${data.transfer}</span> 协议访问 ${firstAns}，操作进程为 ${data.process}`
    }
  },
  // HSM主机管控
  {
    pid: 'HSM',
    useKey: 'type',
    useValue: 'illegal_access',
    oneString: function (data) {
      let location = getAssetIn(data, data.host_ip)
      location === -1 ? (firstAns = `主机 <span class="asset">${data.host_ip}</span>`) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (sendAns = getDstLocationPTD(data)) : (sendAns = getAsset(data, location))
      return `${sendAns} 发现存在接入 ${firstAns} 的违规行为`
    }
  },
  {
    pid: 'HSM',
    useKey: 'type',
    useValue: 'illegal_outer_access',
    oneString: function (data) {
      let location = getAssetIn(data, data.host_ip)
      location === -1 ? (firstAns = `主机 <span class="asset">${data.host_ip}</span>`) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (sendAns = getDstLocationPTD(data)) : (sendAns = getAsset(data, location))
      return `${firstAns} 发现存在外联 ${sendAns} 的违规行为`
    }
  },
  {
    pid: 'HSM',
    useKey: 'type',
    useValue: 'file_sc',
    oneString: function (data) {
      let location = getAssetIn(data, data.host_ip)
      location === -1 ? (firstAns = `主机<span class="asset">${data.host_ip}</span>`) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      return `${firstAns} 发现存在操作 ${data.op}${data.file[0].name} 等${data.file.length} 违规行为`
    }
  },
  // IDS
  {
    pid: 'IDS',
    useKey: 'etype',
    useValue: 'net-intrusion',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let location = getAssetIn(data, data.src.ip)
      location === -1 ? (firstAns = '') : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (firstAns = '') : (sendAns = getAsset(data, location))
      return `发现网络入侵攻击,相关资产为 ${firstAns} ${sendAns}`
    }
  },
  {
    pid: 'IDS',
    useKey: 'etype',
    useValue: 'record',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let location = getAssetIn(data, data.src.ip)
      location === -1 ? (firstAns = `${getSrcLocationPTD(data)}:${data.src.port}`) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (sendAns = `${getDstLocationPTD(data)}:${data.dst.port}`) : (sendAns = getAsset(data, location))
      return `${sendAns} 发现来自 ${firstAns} 的访问行为`
    }
  },
  {
    pid: 'IDS',
    useKey: 'etype',
    useValue: 'recorder',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let location = getAssetIn(data, data.src.ip)
      location === -1 ? (firstAns = `${getSrcLocationPTD(data)}:${data.src.port}`) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (sendAns = `${getDstLocationPTD(data)}:${data.dst.port}`) : (sendAns = getAsset(data, location))
      return `${sendAns} 发现来自  ${firstAns} 的访问行为`
    }
  },
  {
    pid: 'IDS',
    useKey: 'etype',
    useValue: 'c2',
    summary: function (data) {
      return `发现 ${translateVar(this.useKey, this.useValue)} 传输行为，影响资产 ${getAssetIp(data)}`
    },
    oneString: function (data) {
      let location = getAssetIn(data, data.src.ip)
      location === -1 ? (firstAns = `${getSrcLocationPTD(data)}${data.src.ip}`) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (sendAns = `${getDstLocationPTD(data)}${data.src.ip}`) : (sendAns = getAsset(data, location))
      if (firstAns === '' && sendAns === '') {
        return `发现C&C攻击,病毒名称为${data.malname[0]}`
      } else if (firstAns !== '' && sendAns !== '') {
        return `发现C&C攻击,影响${firstAns}, ${sendAns}, 病毒名称为 <span class="virus">${data.malname[0]}</span>`
      } else {
        return `发现C&C攻击,影响${firstAns}${sendAns}, 病毒名称为 <span class="virus">${data.malname[0]}</span>`
      }
    }
  },
  // TOS
  {
    pid: 'TOS',
    useKey: 'type',
    useValue: 'ac',
    oneString: function (data) {
      let location = getAssetIn(data, data.src.ip)
      let secendAns
      location === -1 ? (firstAns = getSrcLocationPTD(data)) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (secendAns = getDstLocationPTD(data)) : (secendAns = getAsset(data, location))
      if (data.rule === 'accept') {
        return `允许从${firstAns}通过<span class="proto">${data.proto}</span>协议到${secendAns}建立连接`
      } else {
        return `拒绝从${firstAns}通过<span class="proto">${data.proto}</span>协议到${secendAns}建立连接`
      }
    }
  },
  {
    pid: 'TOS',
    useKey: 'type',
    useValue: 'mgmt',
    oneString: function (data) {
      if (data.result === 0) {
        return `在<span class="asset">${data.src.ip}</span>登录用户执行命令${data.op},执行成功`
      } else {
        return `在<span class="asset">${data.src.ip}</span>登录用户执行命令${data.op},执行失败`
      }
    }
  },
  {
    pid: 'TOS',
    useKey: 'type',
    useValue: 'pf',
    oneString: function (data) {
      let location = getAssetIn(data, data.src.ip)
      let secendAns
      location === -1 ? (firstAns = getSrcLocationPTD(data)) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (secendAns = getDstLocationPTD(data)) : (secendAns = getAsset(data, location))
      return `允许${firstAns}通过<span class="proto">${data.proto}</span>协议访问${secendAns}`
    }
  },
  {
    pid: 'TOS',
    useKey: 'type',
    useValue: 'system',
    oneString: function (data) {
      return data.msg
    }
  },
  {
    pid: 'TOS',
    useKey: 'type',
    useValue: 'conn',
    oneString: function (data) {
      let location = getAssetIn(data, data.src.ip)
      let secendAns
      location === -1 ? (firstAns = getSrcLocationPTD(data)) : (firstAns = getAsset(data, location))
      location = getAssetIn(data, data.dst.ip)
      location === -1 ? (secendAns = getDstLocationPTD(data)) : (secendAns = getAsset(data, location))
      if (data.op === 'delete') {
        return `关闭从${firstAns}通过 <span class="proto">${data.proto}</span> 协议到${secendAns} 的连接`
      } else {
        return `创建从${firstAns}通过 <span class="proto">${data.proto}</span> 协议到${secendAns} 的连接`
      }
    }
  },
  // mobile
  {
    pid: 'mobile',
    useKey: 'type',
    useValue: 'mobile_flow',
    summary: function (data) {
      return `${getMalicious(data)}`
    },
    oneString: function (data) {
      return `移动终端 <span class="link">${data.src.ip}</span>（${getLocationInfo(data.src)}）正在与 移动终端 <span class="link">${data.dst.ip}</span>（${getLocationInfo(data.dst)}）进行通信，发现${getMd5(data)}恶意代码传输行为，恶意代码名称为${getMaliciou(data)}`
    }
  },
  {
    pid: 'mobile',
    useKey: 'type',
    useValue: 'mobile_endpoint',
    summary: function (data) {
      return `${getMalicious(data)}`
    },
    oneString: function (data) {
      return `发现移动终端<span class="link">${data.host.ip}</span>（${getLocationInfo(data.host)}）遭受恶意代码攻击，恶意代码名称为 ${getMaliciou(data)}, MD5为 ${getMd5(data)}`
    }
  },
  // webray
  {
    pid: 'antiy-ms',
    useKey: 'type',
    useValue: 'visible_dns',
    summary: function (data) {
      let visible = '可用'
      visible = data.alert_status === 'alert' ? '不可用' : visible
      return `发现 <span class="link">${data.site} </span></span> 网站${visible}`
    },
    oneString: function (data) {
      let visible = '可用'
      visible = data.alert_status === 'alert' ? '不可用' : visible
      return `网站 <span class="link">${data.site}</span> ${visible}，${translateVar(this.useKey, this.useValue)}的响应时间为 ${data.visible_time}ms，最后一次响应时间为 ${data.ts}`
    }
  },
  {
    pid: 'antiy-ms',
    useKey: 'type',
    useValue: 'visible_http',
    summary: function (data) {
      let visible = '可用'
      visible = data.alert_status === 'alert' ? '不可用' : visible
      return `发现 <span class="link">${data.site}</span> 网站${visible}`
    },
    oneString: function (data) {
      let visible = '可用'
      visible = data.alert_status === 'alert' ? '不可用' : visible
      return `网站 <span class="link">${data.site}</span> ${visible}，${translateVar(this.useKey, this.useValue)}的响应时间为 ${data.visible_time}ms，最后一次响应时间为 ${data.ts}`
    }
  },
  {
    pid: 'antiy-ms',
    useKey: 'type',
    useValue: 'visible_ping',
    summary: function (data) {
      let visible = '可用'
      visible = data.alert_status === 'alert' ? '不可用' : visible
      return `发现 <span class="link">${data.site}</span> 网站${visible}`
    },
    oneString: function (data) {
      let visible = '可用'
      visible = data.alert_status === 'alert' ? '不可用' : visible
      return `网站 <span class="link">${data.site}</span> ${visible}，${translateVar(this.useKey, this.useValue)}的响应时间为 ${data.visible_time}ms，最后一次响应时间为 ${data.ts}`
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'threat_trojan',
    summary: function (data) {
      return `网站 <span class="link">${data.site}</span> 存在网页挂马`
    },
    oneString: function (data) {
      return `网站 <span class="link">${data.site}</span> 的网页 <span class="link">${data.url}</span> 存在网页挂马`
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'threat_phish',
    summary: function (data) {
      return `网站 <span class="link">${data.site}</span> 疑似存在钓鱼网页`
    },
    oneString: function (data) {
      return `网站 <span class="link">${data.site}</span> 的网页 <span class="link">${data.url}</span> 疑似存在钓鱼网页，链接为 <span class="virus">${data.phishurl}</span>`
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'web_vul',
    summary: function (data) {
      return `网站 <span class="link">${data.site}</span> 存在web漏洞`
    },
    oneString: function (data) {
      if (!isNone(data.vulns) && data.vulns.length !== 0) {
        let loopName = []
        let loopNameStr = ''
        for (let el of data.vulns) {
          loopName.push(el.name)
        }
        for (let name of new Set(loopName)) {
          loopNameStr = !isNone(name) ? `${loopNameStr}, ${name}` : loopNameStr
        }
        return `网站 <span class="link">${data.site}</span> 的网页 <span class="link">${transformNone(data.url)}</span> 存在 web漏洞，漏洞名称为 <span class="virus">${loopNameStr}</span>`
      } else {
        return `网站 <span class="link">${data.site}</span> 的网页 <span class="link">${transformNone(data.url)}</span> 存在 web漏洞`
      }
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'os_vul',
    summary: function (data) {
      return `网站 <span class="link">${data.site}</span> 存在系统漏洞`
    },
    oneString: function (data) {
      if (!isNone(data.vulns) && data.vulns.length !== 0) {
        let str = ''
        for (let el of data.vulns) {
          str = !isNone(el.name) ? `${str}, ${el.name}` : str
        }
        return `网站 <span class="link">${data.site}</span> 存在 系统漏洞，漏洞名称为 <span class="virus">${str.substr(1)}</span>`
      } else {
        return `网站 <span class="link">${data.site}</span> 存在 系统漏洞`
      }
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'weak_pwd',
    summary: function (data) {
      if (!isNone(data.crack) && data.crack.length !== 0) {
        return `主机 <span class="asset">${data.crack[0].site_host.ip}</span> 存在弱口令`
      }
    },
    oneString: function (data) {
      if (!isNone(data.crack) && data.crack.length !== 0) {
        return `主机<span class="asset"> ${data.crack[0].site_host.ip}</span> 存在弱口令，通过其 ${data.crack[0].port} 端口的 ${data.crack[0].service}服务 可进行破解，破解账户为 ${data.crack[0].login}，口令为 ${data.crack[0].password}`
      }
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'sensitive',
    summary: function (data) {
      return `发现网站 <span class="link">${data.site}</span> 存在敏感词`
    },
    oneString: function (data) {
      return `网站 <span class="link">${data.site}</span> 的网页 <span class="link">${data.url}</span> 存在 <span class="virus">${data.keyword}</span> 等敏感词`
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'darklink',
    summary: function (data) {
      return `网站 <span class="link">${data.site}</span> 疑似存在暗链`
    },
    oneString: function (data) {
      return `网站 <span class="link">${data.site}</span> 的网页 <span class="link">${data.url}</span> 疑似存在暗链，链接为 <span class="link">${data.link}</span>`
    }
  },
  {
    pid: 'webray',
    useKey: 'type',
    useValue: 'mis',
    summary: function (data) {
      return `网站  <span class="link">${data.site}</span> 的内容疑似发生篡改`
    },
    oneString: function (data) {
      return `网站 <span class="link">${data.site}</span> 的网页  <span class="link">${data.url}</span>  的内容疑似发生篡改`
    }
  },
  {
    pid: 'ASIAINFO',
    useKey: 'pid',
    useValue: 'ASIAINFO',
    summary: function (data) {
      let des = `发现${translateVar('type', data.type)}行为`
      if (data.type === 'none' || data.type === 'undefined') {
        des = '暂未发现任何行为'
      }
      return des
    },
    oneString: function (data) {
      return `${data.msg}`
    }
  },
  {
    pid: 'WAF',
    useKey: 'pid',
    useValue: 'WAF',
    summary: function (data) {
      return `发现${translateVar('type', data.type)}行为`
    },
    oneString: function (data) {
      return `${data.msg}`
    }
  },
  {
    pid: 'DPTECH',
    useKey: 'pid',
    useValue: 'DPTECH',
    summary: function (data) {
      return `发现${translateVar('type', data.type)}`
    },
    oneString: function (data) {
      return `${data.msg}`
    }
  }
]

/**
 * 获取对应类型的描述
 *
 * @param {Object} rawData  原始数据
 * @param {String} type  期望获得的描述的类型['des', 'summary']; 默认'des' 返回描述
 */
export const getOneStringDes = (rawData, type = 'des') => {
  let data = JSON.parse(JSON.stringify(rawData))
  for (let idx = 0, len = desConfig.length; idx < len; idx++) {
    // desConfig[idx].useKey 有 etype , pid, type
    let _type = Object.prototype.toString.call(data[desConfig[idx].useKey])
    data[desConfig[idx].useKey] = _type === '[object Array]' ? data[desConfig[idx].useKey].toString() : data[desConfig[idx].useKey]
    if (desConfig[idx].pid === data.pid && desConfig[idx].useValue === data[desConfig[idx].useKey]) {
      if (type === 'des') {
        return desConfig[idx].oneString(data)
      } else if (type === 'summary') {
        return desConfig[idx].summary(data)
      }
    }
  }
}
